package com.smyh.protcol.host;

import com.smyh.UIM.host.state.GroupActivity;
import com.smyh.UIM.host.state.HostStateActivity;
import com.smyh.bean.HostState;
import com.smyh.bean.PointObjInfo;
import com.smyh.cache.PointValueConstateCache;
import com.smyh.cache.UIM.host.GroupDeviceData;

import org.apache.mina.core.buffer.IoBuffer;

import java.util.HashMap;
import java.util.Map;

public class ProtocolBusyDataProgress {

    public static void dealSingleDigital(byte[] linkAddr, byte vsq, IoBuffer content) throws Exception {
        // 数据数量
        int counts = vsq & 0x7f;
        // 数据点位是否连续-线性
        boolean isLine = (vsq & 0x80) != 0 ? true : false;
        boolean noMapPoint = false;
        String noParsePoint = "";
        Map<Integer, Map<Integer, Integer>> groupMap = new HashMap<>();
        Map<Integer, Integer> map = new HashMap<>();

        int pointV = (content.get(0) & 0xff) + (content.get(1) & 0xff) * 256 - 1;
        for (int i = 0; i < counts; i++) {
            Integer value = -1;
            if (isLine) {
                pointV++;
                value = content.get(i + 2) & 0x1;
            } else {
                pointV = (content.get(i * 3) & 0xff) + (content.get(i * 3 + 1) & 0xff) * 256;
                value = content.get(i * 3 + 2) & 0xff;
            }
            PointObjInfo pointInfo = PointValueConstateCache.pointDigitalMap.get(pointV);
            if (pointInfo.getGroupNum() == null || pointInfo.getDataPoint() == null) {
                noParsePoint += ":" + pointV;
                noMapPoint = true;
                continue;
            }
            int dataPoint = pointInfo.getDataPoint();
            Integer groupNum = pointInfo.getGroupNum();
            map .put(dataPoint, value);
            groupMap.put(groupNum, map);
        }

        GroupDeviceData.putDigitModel(groupMap);
        GroupActivity.refreshHostState();
        if (noMapPoint) {
          //  UIElement.showMessageDialog("请读取不存在点位" + noParsePoint);
        }
    }
    /**
     * 解析标度化遥测数据
     *
     * @param linkAddr
     * @param vsq
     * @param content
     * @throws Exception
     */
    public static void dealStandMock(byte[] linkAddr, byte vsq, IoBuffer content) {
        // 数据数量
        int counts = vsq & 0x7f;
        // 数据点位是否连续-线性
        boolean isLine = (vsq & 0x80) != 0 ? true : false;
        boolean noMapPoint = false;
        String noParsePoint = "";

        Map<Integer, Map<Integer, Integer>> groupMap = new HashMap<>();
        Map<Integer,Integer> map = new HashMap<>();
        int pointV = (content.get(0) & 0xff) + (content.get(1) & 0xff) * 256 - 1;
        for (int i = 0; i < counts; i++) {
            Integer value = -1;
            if (isLine) {
                pointV++;
                value = (content.get(i * 3 + 2) & 0xff) + (content.get(i * 3 + 2 + 1) & 0xff) * 256;
            } else {
                pointV = (content.get(i * 5) & 0xff) + (content.get(i * 5 + 1) & 0xff) * 256;
                value = (content.get(i * 5 + 2) & 0xff) + (content.get(i * 5 + 2 + 1) & 0xff) * 256;
            }
            PointObjInfo pointInfo = PointValueConstateCache.pointMockMap.get(pointV);
            if (pointInfo.getGroupNum() == null || pointInfo.getDataPoint() == null) {
                noParsePoint += ":" + pointV;
                noMapPoint = true;
                continue;
            }
            int dataPoint = pointInfo.getDataPoint();
            Integer groupNum = pointInfo.getGroupNum();
            map .put(dataPoint, value);
            groupMap.put(groupNum, map);
        }

        GroupDeviceData.putMockModel(groupMap);
        GroupActivity.refreshHostState();
        if (noMapPoint) {
//            UIElement.showMessageDialog("请读取不存在点位" + noParsePoint);
        }
    }
    public static void dealHostStandMock(byte[] linkAddr, byte vsq, IoBuffer content) {
        // 数据数量
        int counts = vsq & 0x7f;
        // 数据点位是否连续-线性
        boolean isLine = (vsq & 0x80) != 0 ? true : false;
//		boolean noMapPoint = false;
//		String noParsePoint = "";

        Integer signalIntensity = null;
        Integer  chargeVoltage = null;
        Integer batteryElecVolt = null;

        int pointV = (content.get(0) & 0xff) + (content.get(1) & 0xff) * 256 - 1;

        for (int i = 0; i < counts; i++) {
            Integer value = -1;
            if (isLine) {
                pointV++;
                value = (content.get(i * 3 + 2) & 0xff) + (content.get(i * 3 + 2 + 1) & 0xff) * 256;
            } else {
                pointV = (content.get(i * 5) & 0xff) + (content.get(i * 5 + 1) & 0xff) * 256;
                value = (content.get(i * 5 + 2) & 0xff) + (content.get(i * 5 + 2 + 1) & 0xff) * 256;
            }
            switch (pointV) {
                case 0x9001:{
                    batteryElecVolt = value;
                    break;
                }

                case 0x9002:{
                    signalIntensity = value;
                    break;
                }

                case 0x9003:{
                    chargeVoltage = value;
                    break;
                }

                default:
                    break;
            }
        }

        HostState obj = new HostState();
        obj.setBatteryElecVolt(batteryElecVolt);
        obj.setChargeVoltage(chargeVoltage);
        obj.setSignalIntensity(signalIntensity);

        HostStateActivity.refreshHostState(obj);

//		if (noMapPoint) {
//			UIElement.showMessageDialog("请读取不存在点位" + noParsePoint);
//		}
    }

}
